package com.feng.show.service.impl;

import com.feng.show.comm.ServerResponse;
import com.feng.show.dao.AgentMapper;
import com.feng.show.dao.ProductTypeMapper;
import com.feng.show.dao.RfidMapper;
import com.feng.show.entity.*;
import com.feng.show.service.IAddRfidService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("IAddRfidService")
public class AddRfidServiceImpl implements IAddRfidService {

    @Autowired
    RfidMapper mapper;

    @Autowired
    AgentMapper agentMapper;

    @Autowired
    ProductTypeMapper productTypeMapper;



    @Override
    public ServerResponse<String> shipment(String userId, List<String> tids, String agentId) {
        int total = 0;
        int totalExits = 0;


        //先判断数据库有没有重复的，只把没有出过货物的出货


        //先查询没有数据，


        for (int i = 0; i < tids.size(); i++) {
            String item = tids.get(i);

            if (item == null || item.length() == 0) {
                continue;
            }


            //查询，如果出货，然后插入

            RfidExample example = new RfidExample();
            RfidExample.Criteria criteria = example.createCriteria();
            criteria.andTidEqualTo(item);

            List<Rfid> writes = mapper.selectByExample(example);
            //数据库已经存在，不进行提交
            if (writes != null && writes.size() > 0) {
                ++totalExits;
                continue;
            }

            Rfid rfid = new Rfid();
            rfid.setAgentId(Integer.valueOf(agentId));
            rfid.setIncreatTime(new Date());
            rfid.setTid(item);
            rfid.setUserId(Integer.valueOf(userId));
            int flag = mapper.insertSelective(rfid);
            if (flag > 0) {
                ++total;
            }

        }
        if (totalExits > 0 && total == 0) {
            return ServerResponse.createBySuccessMsg("数据已存在，请勿重复提交");
        }
        if (totalExits > 0 && total != 0) {
            return ServerResponse.createBySuccessMsg(totalExits + "条数已存在，" + "成功提交" + total + "条");

        }
        return ServerResponse.createBySuccessMsg("成功提交" + total + "条");


    }

    @Override
    public ServerResponse<String> shipment(String userId, List<String> tids, String agentId, Integer productId) {
        int total = 0;
        int totalExits = 0;


        //先判断数据库有没有重复的，只把没有出过货物的出货


        //先查询没有数据，
        System.out.println("tids----->"+tids.size());


        for (int i = 0; i < tids.size(); i++) {
            String item = tids.get(i);

            if (item == null || item.length() == 0) {
                continue;
            }


            //查询，如果出货，然后插入

            RfidExample example = new RfidExample();
            RfidExample.Criteria criteria = example.createCriteria();
            criteria.andTidEqualTo(item);
            criteria.andFlagDelEqualTo("0");

            List<Rfid> writes = mapper.selectByExample(example);
            //数据库已经存在，不进行提交
            if (writes != null && writes.size() > 0) {
                System.out.println("已经存在的tid---->"+item);
                ++totalExits;
                continue;
            }

            Rfid rfid = new Rfid();
            rfid.setAgentId(Integer.valueOf(agentId));
            rfid.setIncreatTime(new Date());
            rfid.setTid(item);
            rfid.setProductId(productId);
            rfid.setUserId(Integer.valueOf(userId));

            int flag = mapper.insertSelective(rfid);
            if (flag > 0) {
                ++total;
            }

        }
        if (totalExits > 0 && total == 0) {
            return ServerResponse.createBySuccessMsg("数据已存在，请勿重复提交");
        }
        if (totalExits > 0 && total != 0) {
            return ServerResponse.createBySuccessMsg(totalExits + "条数已存在，" + "成功提交" + total + "条");

        }

        return ServerResponse.createBySuccessMsg("成功提交" + total + "条");

    }

    @Override
    public ServerResponse<List<RfidAgent>> getRfidListInfoByNos(List<String> list, Integer userId) {
        if(list==null||list.size()<=0){
            return ServerResponse.createByErrorMsg("请扫描标签");
        }else{
            List<RfidAgent> all=new ArrayList<>();

            for(int i=0;i<list.size();i++){

                RfidAgent rfidAgentItem=new RfidAgent();
                //
                String tid=list.get(i);
                rfidAgentItem.setTid(tid);

                RfidExample example = new RfidExample();
                RfidExample.Criteria criteria = example.createCriteria();
                criteria.andTidEqualTo(tid);
                criteria.andFlagDelEqualTo("0");

                List<Rfid> rfids = mapper.selectByExample(example);
                //数据库已经存在，不进行提交
                if (rfids != null && rfids.size() > 0) {
                    Rfid rfidItem=rfids.get(0);
                    Integer agentId=rfidItem.getAgentId();
                    Agent agent= agentMapper.selectByPrimaryKey(agentId);


                    rfidAgentItem.setAgent(agent);
                    rfidAgentItem.setIncreatTime(rfidItem.getIncreatTime());

                    //产品名称
                   Integer  productId= rfidItem.getProductId();
                   if(productId!=null){
                       ProductType productTypeItem= productTypeMapper.selectByPrimaryKey(productId);
                       rfidAgentItem.setProductType(productTypeItem);
                   }

                    all.add(rfidAgentItem);
                }else{
                    all.add(rfidAgentItem);
                    continue;
                }

            }
            if(all==null||all.size()==0){
                return ServerResponse.createBySuccessMsg("未查询到数据");
            }
            return ServerResponse.createBySuccesMsg(all);
        }
    }

  @Override
  @Transactional
  public ServerResponse<String> delRfidList(List<String> rfids, Integer userId) {
      int delSum=0;
      Date date=new Date();

      //
    RfidExample example ;
    for(int i=0;i<rfids.size();i++){
      String tid=rfids.get(i);
      example = new RfidExample();
      RfidExample.Criteria criteria = example.createCriteria();
      criteria.andTidEqualTo(tid);
      List<Rfid> rfidList=mapper.selectByExample(example);
      if(rfidList!=null &&rfidList.size()>0){

        //查询
       Rfid rfid0= rfidList.get(0);
       //更新
        if(rfid0.getFlagDel().equals("0")){
          rfid0.setUpdataTime(date);
          rfid0.setFlagDel("2");
          int statue= mapper.updateByPrimaryKey(rfid0);
          delSum=delSum+1;
        }
      }else{

      }
    }


//    RfidExample example = new RfidExample();
//    RfidExample.Criteria criteria = example.createCriteria();
//    criteria.andTidEqualTo(item)

    if(delSum==0){
      return ServerResponse.createBySuccessMsg("没找到要退货的商品");
    }
    return ServerResponse.createBySuccessMsg("成功退货"+delSum+"条");
  }
}
